﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RCREEE.Library.Entities;

namespace RCREEE.Library
{
    public static partial class DataManager
    {
        public static DataTable GetOneApprovedIncentives(int CountryId, int LanguageCode, int ItemId)
        {
            try
            {
                _Adapter.SelectCommand.CommandText = "GetOneApprovedIncentives";
                _Adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                _Adapter.SelectCommand.Parameters.Clear();

                _Adapter.SelectCommand.Parameters.Add("@CategoryId", SqlDbType.Int).Value = 10;
                _Adapter.SelectCommand.Parameters.Add("@CountryId", SqlDbType.Int).Value = CountryId;
                _Adapter.SelectCommand.Parameters.Add("@LanguageCode", SqlDbType.Int).Value = LanguageCode;
                _Adapter.SelectCommand.Parameters.Add("@ItemId", SqlDbType.Int).Value = ItemId;
                _DataTable = new DataTable();
                _Adapter.Fill(_DataTable);

                if (_DataTable.Rows.Count == 0 && ItemId == 0)
                { return null; }
                else
                    _DataTable.Rows.Add(_DataTable.NewRow());

                return _DataTable; 
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public static DataTable GetApprovedIncentives(int CountryId, int LanguageCode)
        {
            try
            {
                _Adapter.SelectCommand.CommandText = "GetApprovedIncentives";
                _Adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                _Adapter.SelectCommand.Parameters.Clear();
               
                _Adapter.SelectCommand.Parameters.Add("@CategoryId", SqlDbType.Int).Value = 10;
                _Adapter.SelectCommand.Parameters.Add("@CountryId", SqlDbType.Int).Value = CountryId;
                _Adapter.SelectCommand.Parameters.Add("@LanguageCode", SqlDbType.Int).Value = LanguageCode;

                _DataTable = new DataTable();
                _Adapter.Fill(_DataTable);

                if (_DataTable.Rows.Count != 0 && CountryId == 0)
                { return null; }
                else
                    _DataTable.Rows.Add(_DataTable.NewRow());

                 return _DataTable;
            }
            catch (Exception e)
            {
                return _DataTable;
            }
        }

        public static int UpdateApprovedIncentives(List<ApprovedIncentives> app)
        {

            try
            {
                _Command.CommandText = "InsertUpdateAprrovalPercentage";
                _Command.CommandType = CommandType.StoredProcedure;
                _Command.Parameters.Clear();
                _Command.Parameters.Add("@ApprovedIncentivesId", SqlDbType.Int).Value = app[0].ApprovedIncentiveId;
                _Command.Parameters.Add("@IncentiveId", SqlDbType.Int).Value = app[0].IncentiveId;
                _Command.Parameters.Add("@CountryId", SqlDbType.Int).Value = app[0].CountryId;
                _Command.Parameters.Add("@Percentage", SqlDbType.Int).Value = app[0].Percentage;
                _Command.Parameters.Add("@ItemId", SqlDbType.Int).Value = app[0].ItemId;
                int ApprovedIncentiveId;
                if (app[0].ApprovedIncentiveId == 0)
                {
                    OpenConnection();
                    ApprovedIncentiveId = Convert.ToInt32(_Command.ExecuteScalar());
                    CloseConnection();
                }
                else
                {
                    OpenConnection();
                    _Command.ExecuteNonQuery();
                    CloseConnection();
                    ApprovedIncentiveId = app[0].ApprovedIncentiveId;
                }

               

                _Command.CommandText = "InsertUpdateAprroval";
                _Command.CommandType = CommandType.StoredProcedure;
                
                for (int i = 0; i < app.Count; i++)
                {
                    _Command.Parameters.Clear();
                    _Command.Parameters.Add("@ApprovedIncentivesId", SqlDbType.Int).Value = ApprovedIncentiveId;
                    _Command.Parameters.Add("@IncentiveId", SqlDbType.Int).Value = app[i].IncentiveId;
                    _Command.Parameters.Add("@CountryId", SqlDbType.Int).Value = app[i].CountryId;
                    _Command.Parameters.Add("@LanguageCode", SqlDbType.Int).Value = app[i].LanguageCode;
         
                    _Command.Parameters.Add("@Ceiling", SqlDbType.NVarChar).Value = app[i].Ceiling;
                    _Command.Parameters.Add("@ItemId", SqlDbType.Int).Value = app[i].ItemId;
                    OpenConnection();
                    _Command.ExecuteNonQuery();
                    CloseConnection();
                }
               
                return 1;

            }
            catch (Exception)
            { return 0; }
        }

        public static int InsertApprovedIncentives(ApprovedIncentives app)
        {

            try
            {
                _Command.CommandText = "";
                _Command.CommandType = CommandType.StoredProcedure;
                _Command.Parameters.Clear();

                _Command.Parameters.Add("@ApprovedIncentiveId", SqlDbType.Int).Value = app.ApprovedIncentiveId;
                _Command.Parameters.Add("@IncentiveId", SqlDbType.Int).Value = app.IncentiveId;
                _Command.Parameters.Add("@CountryId", SqlDbType.Int).Value = app.CountryId;
                _Command.Parameters.Add("@LanguageCode", SqlDbType.Int).Value = app.LanguageCode;
                _Command.Parameters.Add("@Percentage", SqlDbType.Int).Value = app.Percentage;
                _Command.Parameters.Add("@Ceiling", SqlDbType.NVarChar).Value = app.Ceiling;

                OpenConnection();
               int roweffect= _Command.ExecuteNonQuery();
                CloseConnection();


                return 1;

            }
            catch (Exception)
            { return 0; }
        }

        public static int DeleteApprovedIncentives(ApprovedIncentives app)
        {

            try
            {
                _Command.CommandText = "";
                _Command.CommandType = CommandType.StoredProcedure;
                _Command.Parameters.Clear();

                _Command.Parameters.Add("@ApprovedIncentiveId", SqlDbType.Int).Value = app.ApprovedIncentiveId;
               
                OpenConnection();
                int roweffect = _Command.ExecuteNonQuery();
                CloseConnection();


                return roweffect;

            }
            catch (Exception)
            { return 0; }
        }
    }
}
